from flask_restful import fields

from app.utils import SimpleDateField

admin_user_fields = {
    'id': fields.Integer,
    'username': fields.String,
    'real_name': fields.String,
    'phone_number': fields.String,
    'last_login_at': fields.String,
    'role': fields.String
}

banner_fields = {
    'id': fields.Integer,
    'category': fields.String,
    'position': fields.Integer,
    'image_url': fields.String,
    'json_data': fields.String,
    'branch_id': fields.String,
    'start_time': SimpleDateField,
    'end_time': SimpleDateField,
    'weights': fields.Integer,
    'deleted': fields.Boolean
}

branch_fields = {
    'id': fields.Integer,
    'name': fields.String,
    'manager': fields.String,
    'mobile': fields.String,
    'address': fields.String,
    'admin': fields.List(fields.Nested({
        'id': fields.Integer,
        'username': fields.String,
        'real_name': fields.String,
        'phone_number': fields.String,
        'locked': fields.Boolean,
        'deleted': fields.Boolean
    }))
}

brand_fields = {
    'id': fields.Integer,
    'name': fields.String,
    'description': fields.String
}

category_fields = {
    'id': fields.Integer,
    'name': fields.String,
    'pic_url': fields.String,
    'sort': fields.Integer
}

goods_fields = {
    'id': fields.Integer,
    'name': fields.String,
    'sku': fields.String,
    'brand_id': fields.Integer,
    'brand': fields.Nested({
        'id': fields.Integer,
        'name': fields.String
    }),
    'category_id': fields.Integer,
    'category': fields.Nested({
        'id': fields.Integer,
        'name': fields.String
    }),
    'secondary_category_id': fields.Integer,
    'secondary_category_name': fields.String(attribute='secondary_category.name'),
    'detail': fields.String,
    'pic_url': fields.String,
    'exchange_price': fields.Integer,
    'total_num': fields.Integer,
    'barcode': fields.String,
    'deleted': fields.Boolean,
    'created': SimpleDateField,
    'images': fields.List(fields.Nested({
            'id': fields.Integer,
            'url': fields.String,
            'width': fields.Integer,
            'height': fields.Integer
    })),
    'info': fields.List(fields.Nested({
        'id': fields.Integer,
        'branch_id': fields.Integer,
        'weights': fields.Integer,
        'is_listing': fields.Boolean,
        'recommended': fields.Boolean,
        'stock': fields.Integer,
        'exchange_num': fields.Integer,
    })),
    'is_online': fields.Integer,
    'online_time': fields.String,
    'period': fields.Integer,
    'required_number': fields.Integer,
    'template_id': fields.Integer,
    'fee_template': fields.Nested({
        'id': fields.Integer,
        'name': fields.String,
        'base_fee': fields.Integer,
        'add_fee': fields.Integer
    }, attribute='fee_template'),
    'sku_attrs': fields.List(fields.Nested({
        'k_id': fields.Integer,
        'k_name': fields.String,
        'values': fields.List(fields.Nested({
            'v_id': fields.Integer,
            'v_name': fields.String
        }))
    })),
    'skus': fields.List(fields.Nested({
        'id': fields.Integer,
        'sku_sn': fields.String,
        'price':  fields.Integer,
        'cover': fields.String,
        'attrs': fields.List(fields.Nested({
            'k_id': fields.Integer,
            'k_name': fields.String,
            'v_id': fields.Integer,
            'v_name': fields.String,
        }))
    })),
}

info_fields = {
    'id': fields.Integer,
    'branch_id': fields.Integer,
    'branch': fields.Nested({
        'id': fields.Integer,
        'name': fields.String
    }),
    'goods_id': fields.Integer,
    'weights': fields.Integer,
    'is_listing': fields.Boolean,
    'recommended': fields.Boolean,
    'stock': fields.Integer,
    'sku_stock': fields.List(fields.Nested({
        'id': fields.Integer,
        'sku_id': fields.Integer,
        'stock': fields.Integer
    })),
    'sku_attrs': fields.List(fields.Nested({
        'k_id': fields.Integer,
        'k_name': fields.String,
        'value_list': fields.List(fields.Nested({
            'v_id': fields.Integer,
            'v_name': fields.String
        }))
    }), attribute='sku_attrs'),
    'tags': fields.List(fields.Nested(
        {
            'id': fields.Integer,
            'name': fields.String
        }
    )),
}

order_fields = {
    'id': fields.Integer,
    'order_no': fields.String,
    'goods_name': fields.String(attribute='goods.name'),
    'user_id': fields.Integer,
    'nickname': fields.String(attribute='user.nickname'),
    'branch_id': fields.Integer,
    'branch_name': fields.String(attribute='branch.name'),
    'order_time': SimpleDateField,
    'exchange_type': fields.Integer,
    'status': fields.Integer,
    'amount': fields.Integer,
    'unit_price': fields.Integer,
    'total_price': fields.Integer,
    'address': fields.Nested({
        'id': fields.Integer,
        'name': fields.String,
        'phone': fields.String,
        'address': fields.String,
        'province': fields.String,
        'city': fields.String,
        'county': fields.String
    }),
    'pic_url': fields.String(attribute='goods.pic_url'),
    'courier_company': fields.String,
    'courier_no': fields.String,
    'remark': fields.String,
    'tags': fields.List(fields.Nested(
        {
            'id': fields.Integer,
            'name': fields.String
        }
    ), attribute='goods.tags'),
    'sku_attrs': fields.List(fields.Nested({
        'k_id': fields.Integer,
        'k_name': fields.String,
        'v_id': fields.Integer,
        'v_name': fields.String
    }), attribute='order_sku.sku.attrs'),
}

user_fields = {
    'id': fields.Integer,
    'uid': fields.String,
    'nickname': fields.String,
    'avatar': fields.String,
    'branch': fields.Nested(branch_fields),
    'doll': fields.Integer,
    'status': fields.Boolean,
    'gender': fields.Integer,
    'created': SimpleDateField,
}

tag_fields = {
    'id': fields.Integer,
    'name': fields.String,
    'exchange_type': fields.Integer
}

fee_template_fields = {
    'id': fields.Integer,
    'name': fields.String,
    'base_fee': fields.Integer,
    'add_fee': fields.Integer
}

remote_area_fields = {
    'code': fields.String,
    'name': fields.String
}

attrs_fields = {
    'k_id': fields.Integer(attribute='id'),
    'k_name': fields.String(attribute='name'),
    'values': fields.List(fields.Nested({
        'v_id': fields.Integer(attribute='id'),
        'v_name': fields.String(attribute='name')
    }))
}

attr_fields = {
    'k_id': fields.Integer(attribute='id'),
    'k_name': fields.String(attribute='name')
}

attr_value_fields = {
    'v_id': fields.Integer(attribute='id'),
    'v_name': fields.String(attribute='name')
}
